package com.example.goodpet.my.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.goodpet.R;
import com.example.goodpet.base.VPMFragmentActivity;
import com.example.goodpet.community.bean.UserTieziInfo;
import com.example.goodpet.my.adapter.PetHistoryAdapter;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfoAndHistory;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.db.DatabaseHelper;
import com.example.goodpet.db.dao.GoodpetHistoryDAO;
import com.example.goodpet.db.entity.GoodpetHistory;
import com.example.goodpet.my.fragment.MyHistoryPetFragment;
import com.example.goodpet.my.fragment.MyHistoryTieziFragment;
import com.example.goodpet.my.fragment.MyTieziInfoFragment;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.ActivityManager;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MyHistoryActivity extends VPMFragmentActivity {

    @Override
    public void initTitle() {
            cmTitleText.setText("浏览历史");
            cmTextRight.setBackground(null);
            cmTextRight.setText("清空");
    }

    @Override
    public void setFragmentList() {
        fragmentList=new ArrayList<>();
        fragmentList.add(new MyHistoryPetFragment());
        fragmentList.add(new MyHistoryTieziFragment());
    }
    @Override
    public void initListener() {
        cmTitleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManager.getInstance().removeCurrent();
            }
        });
        cmTextRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MyHistoryActivity.this)
                        .setTitle("温馨提示")
                        .setMessage("您确定要删除宠物信息和帖子的浏览记录吗？")
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                clearContent();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        })
                        .setCancelable(false).show();
            }
        });

    }

    @Override
    protected int setTabTitle() {
        return R.array.user_info_tab;
    }
    private void clearContent(){
        ((MyHistoryPetFragment) fragmentList.get(0)).clear();
        ((MyHistoryTieziFragment) fragmentList.get(1)).clear();
        UIUtils.toast("清除成功",false);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK)
            if (requestCode==1){
                UserTieziInfo info = data.getParcelableExtra("info");
                int position = data.getIntExtra("position", -1);
                if (position!=-1 && info!=null) ((MyHistoryTieziFragment)fragmentList.get(1)).refresh(position,info);
            }
    }
}